﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class LoadScreen : GenericScreenCode
    {
        bool SlowLoading;

        bool oneScreenLeft;

        GenericScreenCode[] screensToLoad;

        public LoadScreen(SManager sManager, bool slowLoading, GenericScreenCode[] screens)
        {
            SlowLoading = slowLoading;
            screensToLoad = screens;

        }

        public static void Load(SManager sManager, bool slowLoading, PlayerIndex? calledPlayer,
            params GenericScreenCode[] screens)
        {
            int cnt = 0;
            foreach (GenericScreenCode screen in sManager.GetScreens())
            {
                cnt++;
                screen.ExitScreen();
            }

            LoadScreen newLoad = new LoadScreen(sManager, slowLoading, screens);

            sManager.AddScreen(newLoad, calledPlayer);
            //sManager.AddScreen(new TitleScreen(), calledPlayer);
        }

        public override void Update(GameTime gameTime, bool setFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, setFocus, coveredByOtherScreen);

           

            if (oneScreenLeft)
            {
                SManager.RemoveScreen(this);

                foreach (GenericScreenCode screen in screensToLoad)
                {
                    SManager.AddScreen(screen, ControllingPlayer);
                }
            }

            if ((ScreenState == ScreenState.Active) &&
               (SManager.GetScreens().Length == 1))
            {
                oneScreenLeft = true;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);

            if (SlowLoading)
            {
                SpriteBatch spriteBatch = SManager.SpriteBatch;
                const string text = "Gettin' Loaded";

                SpriteFont font = SManager.SpriteFont;

                Viewport view = SManager.GraphicsDevice.Viewport;

                Vector2 size = new Vector2(view.Width, view.Height);

                Vector2 SizeOfText = font.MeasureString(text);

                Vector2 textPos = (size - SizeOfText) / 2;

                spriteBatch.Begin();
                //spriteBatch.DrawString(font, text, textPos, Color.White);
                spriteBatch.DrawString(font, text, textPos, Color.White);
                spriteBatch.End();

            }
        }


    }
}
